#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
typedef struct
{
    int head;
    int k;
    int tail;
    int* a;
} MCQueue;
MCQueue* MCQueueCreat(int k)
{
    MCQueue* obj=(MCQueue*)malloc(sizeof(MCQueue));
    obj->head=0;
    obj->tail=0;
    obj->a = malloc(sizeof(int)*(k+1));
    obj->k=k;

    return obj;
}

bool MCQueueIempty(MCQueue* obj)
{
    return obj->head== obj->tail;
}
bool MCQueueIsfull(MCQueue* obj)
{
    return (obj->tail+1)%(obj->k+1) ==obj->head;
}
bool MCQueueEnqueue(MCQueue* obj,int value)
{
    if(MCQueueIsfull(obj))
    {
        return false;
    }
    obj->a[obj->tail]=value;
    obj->tail++;
    obj->tail%=(obj->k+1);
    return true;
}


int MCQueueRear(MCQueue* obj)
{
    if(MCQueueIempty(obj))
    {
        return -1;
    }
    else
    {
        return obj->a[(obj->tail-1+obj->k+1)%(obj->k+1)];
    }

}

bool MCQueueDequeue(MCQueue* obj)
{
    if(MCQueueIempty(obj))
    {
        return false;
    }
    ++obj->head;
    obj->head%=(obj->k+1);
    return true;

}

int MCQueueFront(MCQueue*obj)
{
    if(MCQueueIempty(obj))
        return -1;
    else
        return obj->a[obj->head];
}


bool MCQueueFree(MCQueue* obj)
{
    free(obj->a);
    free(obj);
}
bool MCQueuePrint(MCQueue* obj)
{
    if(MCQueueIempty(obj))
        printf("็ฉบ");


    int i=obj->head;
    while(i!=obj->tail)
    {
        printf("%d",obj->a[i]);
        i=(i+1)%(obj->k+1);

    }
    printf("\n");
}
void MCQueueTest()
{
    MCQueue* q =MCQueueCreat(5);
    MCQueueEnqueue(q,1);
    MCQueueEnqueue(q,2);
    MCQueueEnqueue(q,3);
    MCQueueEnqueue(q,4);
    MCQueueEnqueue(q,5);
    MCQueuePrint(q);
    // MCQueueDequeue(q);
    //  MCQueuePrint(q);
    int headval=MCQueueFront(q);
    printf("%d\n",headval);
    int rearval=MCQueueRear(q);
    printf("%d",rearval);

    MCQueueFree(q);


}


int main()
{
    MCQueueTest();
    return 0;
}